package com.xuan.webfinally.controller;

import com.ldyxjc.entity.DeviceStatus;
import com.xuan.webfinally.entity.WarningInfo;
import com.xuan.webfinally.entity.WarningStats;
import com.xuan.webfinally.service.ISlopeDeviceStatusService;
import com.xuan.webfinally.service.ISlopeInfoService;
import com.xuan.webfinally.service.ISlopeWarningInfoService;
import com.xuan.webfinally.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ldyxjc")
public class LdyxjcController {


    @Autowired
    private ISlopeDeviceStatusService  slopeDeviceStatusService;

    @Autowired
    private ISlopeWarningInfoService slopeWarningInfoService;

    @GetMapping("/device-status")
    public Result<Map<String, Object>> getDeviceStatus(HttpServletRequest request) {
        String username = request.getHeader("X-Username");
        Long deptid = Long.valueOf(request.getHeader("X-DeptId"));
        System.out.println("当前登录用户：" + username);

        Map<String, Object> result = new HashMap<>();
        List<DeviceStatus> data = new ArrayList<>();
        
        //添加示例数据
        DeviceStatus warning = new DeviceStatus();
        warning.setName("预警");
        int value = slopeDeviceStatusService.getYJValue(username,deptid);
        warning.setValue(value);
        warning.setColor("#FF4050");
        data.add(warning);

        DeviceStatus offline = new DeviceStatus();
        offline.setName("离线");
        int offlineValue = slopeDeviceStatusService.getLXValue(username,deptid);
        offline.setValue(offlineValue);
        offline.setColor("#FFFFFF");
        data.add(offline);

        DeviceStatus normal = new DeviceStatus();
        normal.setName("正常");
        int normalValue = slopeDeviceStatusService.getZCValue(username,deptid);
        normal.setValue(normalValue);
        normal.setColor("#04CBE7");
        normal.setOffset(true);
        data.add(normal);
        result.put("data", data);
        System.out.println(result + "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkiiiiiiiiiiiiiiiiiiiii");
        return Result.success(result);
    }

    @GetMapping("/warning-stats")
    public Result<Map<String, Object>> getWarningStats(HttpServletRequest request) {
        String username = request.getHeader("X-Username");
        System.out.println("当前登录用户：" + username);

        Long deptid = Long.valueOf(request.getHeader("X-DeptId"));
        Map<String, Object> result = new HashMap<>();
        WarningStats stats = new WarningStats();
        int total = slopeWarningInfoService.getTotal(username,deptid);
        stats.setTotal(total);
        
//        result.put("code", 200);
        result.put("data", stats);
        return Result.success(result);
    }




    @GetMapping("/warning-list")
    public Result<Map<String, Object>> getWarningList(HttpServletRequest request) {
        String username = request.getHeader("X-Username");
        System.out.println("当前登录用户：" + username);
        Long deptid = Long.valueOf(request.getHeader("X-DeptId"));

        Map<String, Object> result = new HashMap<>();
        List<WarningInfo> data = slopeWarningInfoService.queryWarningList(username,deptid) ;
        // 添加示例数据
//        WarningInfo warning = new WarningInfo();
//        warning.setA("示例路段");
//        warning.setB("示例描述");
//        warning.setC("SENSOR001");
//        warning.setD("区域A");
//        warning.setE("温度监测");
//        warning.setF("温度异常");
//        warning.setG("养护单位A");

//        result.put("code", 200);
        result.put("data", data);
        int total = slopeWarningInfoService.getTotalLd(username,deptid);
        result.put("total", total);
        return Result.success(result);
    }
} 